package hoang.hut.app.utility;

import android.annotation.SuppressLint;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressLint("SimpleDateFormat")
public class TimeUtility {

	/**
	 * Chuyển thời gian từ s sang định dạng string
	 * 
	 * @param time
	 *            : Thời gian tính theo s
	 * @return : Sẽ chuyển thành dạng string có định dạng hh:pp:ss
	 *         (giờ:phút:giây)
	 * 
	 * @example time = 65; => return 01:05
	 */
	public static String getTime(int time) {
		int s = time, p = 0, h = 0;

		h = s / 3600;
		s = s % 3600;
		p = s / 60;
		s = s % 60;

		String gio = "", phut = "", giay = "";

		if (String.valueOf(h).length() < 2)
			gio = "0" + String.valueOf(h);
		else
			gio = String.valueOf(h);
		if (String.valueOf(p).length() < 2)
			phut = "0" + String.valueOf(p);
		else
			phut = String.valueOf(p);
		if (String.valueOf(s).length() < 2)
			giay = "0" + String.valueOf(s);
		else
			giay = String.valueOf(s);

		if (h == 0)
			return phut + ":" + giay;
		else if (p == 0 && h == 0)
			return giay;

		if (gio.equals("00"))
			return phut + ":" + giay;
		return gio + ":" + phut + ":" + giay;
	}

	/**
	 * Get the string of the date using format "yyyy-MM-dd HH:mm:ss"
	 * 
	 * @param date
	 * @return a string formatted from date
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getDateString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		return time;
	}

	/**
	 * get current time using format "yyyy-MM-dd HH:mm:ss"
	 * 
	 * @param timestamp
	 *            : milsecond - System.currentTimeMillis()
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static CharSequence createDate(long timestamp) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(timestamp);
		Date d = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		return sdf.format(d);
	}

	@SuppressLint("SimpleDateFormat")
	public static CharSequence createDate(long timestamp, String format) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(timestamp);
		Date d = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(d);
	}

	/**
	 * Parse a string that contains date, return a date object using format
	 * "yyyy-MM-dd HH:mm:ss"
	 * 
	 * @param dateString
	 * @return a date object that is parsed from dateString
	 */
	public static Date convertString2Date(String dateString, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdf.parse(dateString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * Used to convert 24hr format to 12hr format with AM/PM values
	 * 
	 * @param hours
	 * @param mins
	 */
    public static String convertTo12Hour(int hours, int mins) {
         
        String timeSet = "";
        if (hours > 12) {
            hours -= 12;
            timeSet = "PM";
        } else if (hours == 0) {
            hours += 12;
            timeSet = "AM";
        } else if (hours == 12)
            timeSet = "PM";
        else
            timeSet = "AM";
 
         
        String minutes = "";
        if (mins < 10)
            minutes = "0" + mins;
        else
            minutes = String.valueOf(mins);
 
        // Append in a StringBuilder
         String aTime = new StringBuilder().append(hours).append(':')
                .append(minutes).append(" ").append(timeSet).toString();
 
         return aTime;
    }
}
